# -*- coding: utf-8 -*-


"""
简单工厂模式

模式特点：工厂根据条件产生不同功能的类。

程序实例：四则运算计算器，根据用户的输入产生相应的运算类，用这个运算类处理具体的运算。

代码特点：C/C++中的switch...case...分支使用字典的方式代替。

使用异常机制对除数为0的情况进行处理。
"""


class Operation(object):
    def get_result(self):
        pass


class OperationAdd(Operation):
    def get_result(self):
        return self.op1 + self.op2


class OperationSub(Operation):
    def get_result(self):
        return self.op1 - self.op2


class OperationMul(Operation):
    def get_result(self):
        return self.op1 * self.op2


class OperationDiv(Operation):
    def get_result(self):
        try:
            return self.op1 / self.op2
        except ZeroDivisionError:
            print "error:divided by zero."
            return 0


class OperationUndef(Operation):
    def get_result(self):
        print "Undefine operation."
        return 0


class OperationFactory(object):

    operation = {
        "+": OperationAdd(),
        "-": OperationSub(),
        "*": OperationMul(),
        "/": OperationDiv(),
    }

    def create_operation(self, ch):
        if ch in self.operation:
            op = self.operation[ch]
        else:
            op = OperationUndef()

        return op


if __name__ == "__main__":
    op = raw_input("operator: ")
    opa = input("a: ")
    opb = input("b: ")
    factory = OperationFactory()
    cal = factory.create_operation(op)
    cal.op1 = opa
    cal.op2 = opb
    print cal.get_result()
